<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12 0012
 * Time: 下午 3:07
 */

namespace backend\assets;


use common\components\GlobalUrlComponent;
use yii\web\AssetBundle;

class EChartsAsset extends AssetBundle
{
    public $baseUrl = '';

    public $css = [
        'plugins/bower_components/jquery-datatables-editable/datatables.css'
    ];
    public $js = [
        'plugins/echarts/echarts.common.min.js',
        'plugins/echarts/init.js?v=3',
    ];
    public $depends = [
        AppAsset::class
    ];

    const Y_FIELD = "field";
    const Y_NAME = "name";
    public function __construct(array $config = [])
    {
        $this->baseUrl = GlobalUrlComponent::buildStaticUrl('/');

        parent::__construct($config);
    }

    public static function formatData($ret, $xField, $yField)
    {
        if ( is_string($yField) ) {
            return [
                "xAxis" => array_column($ret, $xField),
                "data" => array_column($ret, $yField),
            ];
        }
        $y = [];
        foreach ( $yField as $key => $item ) {
            $y[$key]["data"] = array_column($ret, $item[self::Y_FIELD]);
            $y[$key]["name"] = $item[self::Y_NAME];
        }
        return [
            "xAxis" => array_column($ret, $xField),
            "series" => $y,
        ];
    }
}